#include "signature.h"
#include "ui_signature.h"
#include <QGraphicsDropShadowEffect>
#include "mainwindow.h"


signature::signature(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::signature)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);

    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);

    //阴影颜色
    shadow_effect->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect->setBlurRadius(10);
    ui->label_4->setGraphicsEffect(shadow_effect);
//    setGeometry(100,100,200,200);

//    ui->label_2->hide();
    stb=new signatureBox();
    connect(stb,SIGNAL(sendBoxSin(QString)),this,SLOT(receBoxSin(QString)));
    stb->setGeometry(35,73,540,280);
    //把此窗口嵌入主窗口
    stb->setParent(this);
    stb->show();

    //设置标题字间距
    QFont font;
    font.setLetterSpacing(QFont::PercentageSpacing,120);//120字间距，默认100
    ui->label->setFont(font);
    font.setFamily(QStringLiteral("Microsoft YaHei"));
    //设置字体大小
    font.setPointSize(12);
    //字体加粗
    font.setWeight(QFont::Normal);
    ui->pushButton->setFont(font);
    ui->pushButton_2->setFont(font);

    setPushbutton(0);//初始化按钮颜色
}

signature::~signature()
{
    delete ui;
}

void signature::receBoxSin(QString data)
{
    if(data == "1")
    {
        ifLine++;
        if(ifLine == 1) setPushbutton(ifLine);
    }
}

//完成按钮
void signature::on_pushButton_2_clicked()
{
    //把签名窗口保存图片到指定位置
//    QPixmap pixmap = QPixmap::grabWidget(stb);
    QPixmap pixmap = stb->grab();
    pixmap.save(QString("%1%2").arg(MY_PATH).arg("签名.jpg"),"JPG");
//    QPixmap::grabWindow(stb->winId(),0, 0, -1, -1).save("C:/lscFile/qtFile/签名.jpg");
    emit sendSignaDestroy("1:yes");
    //关闭当前窗口
    delete this;
}

//清空按钮
void signature::on_pushButton_clicked()
{
    ifLine = 0;
    setPushbutton(ifLine);
    delete stb;
    stb=new signatureBox();
    connect(stb,SIGNAL(sendBoxSin(QString)),this,SLOT(receBoxSin(QString)));
    stb->setGeometry(35,73,540,280);
    //把此窗口嵌入主窗口
    stb->setParent(this);
    stb->show();
}

bool signature::event(QEvent *event)
{
    if (event->type() == QEvent::ActivationChange)
    {
        if(QApplication::activeWindow() != this)
        {
            delete this;
        }
    }
    return QWidget::event(event);
}

//根据画板上是否有线设置按钮颜色
void signature::setPushbutton(int ifline)
{
    if(ifline == 0)
    {
        //没有线
        ui->pushButton->setEnabled(false);//禁用按钮
        ui->pushButton_2->setEnabled(false);
        ui->pushButton->setStyleSheet("background: #F1F6F9;color: #DFDFDF;font: 12pt \"Microsoft YaHei UI\";border-bottom-left-radius :10px;");
        ui->pushButton_2->setStyleSheet("background: #CCCCCC;color: #FBFBFB;font: 12pt \"Microsoft YaHei UI\";border-bottom-right-radius :10px;");
    }
    else if(ifline == 1)
    {
        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton->setStyleSheet("background: #F1F6F9;color: rgb(153, 153, 153);font: 12pt \"Microsoft YaHei UI\";border-bottom-left-radius :10px;");
        ui->pushButton_2->setStyleSheet("background: #1B89CC;color: #FBFBFB;font: 12pt \"Microsoft YaHei UI\";border-bottom-right-radius :10px;");
    }
}

//点击关闭
void signature::on_pushButton_3_clicked()
{
    delete this;
}
